package ru.amse.tsyganov.jumleditor.view.lines;


public class MiddleLine extends Line {
	
	private Line fromLine;
	
	private Line toLine;
	
	public MiddleLine() {
	}
	
	public MiddleLine(Line fromLine, Line toLine) {
		this.fromLine = fromLine;
		this.toLine = toLine;
	}

	public int getX1() {
		if (fromLine instanceof FromConnectionPointLine) {
			return fromLine.getX2();
		} else {
			if (isHorizontal()) {
				return fromLine.getX1();
			} else {
				return toLine.getX1();
			}
		}
	}
	
	public int getY1() {
		if (fromLine instanceof FromConnectionPointLine) {
			return fromLine.getY2();
		} else {
			if (isHorizontal()) {
				return toLine.getY1();
			} else {
				return fromLine.getY1();
			}
		}
	}

	public int getX2() {
		return isHorizontal() ? toLine.getX1() : getX1();
	}

	public int getY2() {
		return isHorizontal() ? getY1() : toLine.getY1();
	}

	public boolean isHorizontal() {
		assert fromLine.isHorizontal() == toLine.isHorizontal();
		return !fromLine.isHorizontal();
	}

	public void setX1(int x1) {
		if (!isHorizontal()) {
			fromLine.setX2(x1);
			toLine.setX1(x1);
		}
	}

	public void setX2(int x2) {
		if (!isHorizontal()) {
			fromLine.setX2(x2);
			toLine.setX1(x2);
		}
	}

	public void setY1(int y1) {
		if (isHorizontal()) {
			fromLine.setY2(y1);
			toLine.setY1(y1);
		} 
	}

	public void setY2(int y2) {
		if (isHorizontal()) {
			fromLine.setY2(y2);
			toLine.setY1(y2);
		}
	}

	public void setFromLine(Line fromLine) {
		this.fromLine = fromLine;
	}

	public void setToLine(Line toLine) {
		this.toLine = toLine;
	}

}
